package com.herewego;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Mapa extends MapActivity {
	 private MapView map=null;
	 private MyLocationOverlay me=null;
	 private final int MENU_ITEM_0 = 0;  
	 private final int MENU_ITEM_1 = 1;  
	 private final int MENU_ITEM_2 = 2;  
	 private final int MENU_ITEM_3 = 3;  
	 private final int MENU_ITEM_4 = 4;  

		public boolean onCreateOptionsMenu(Menu menu) {  

		    menu.add(0, MENU_ITEM_0, 0, "Home").setIcon(R.drawable.tabhome2x);  
		    menu.add(0, MENU_ITEM_1, 0, "Mi viaje").setIcon(R.drawable.tabmiviaje2x);  
		    menu.add(0, MENU_ITEM_2, 0, "Configuración").setIcon(R.drawable.tabajustes2x);  
		    menu.add(0, MENU_ITEM_3, 0, "Camara").setIcon(R.drawable.tabcamara2x);  
		    menu.add(0, MENU_ITEM_4, 0, "Modo").setIcon(R.drawable.tabmodo2x);  
		    return true;  
		}  
		 

		public boolean onOptionsItemSelected(MenuItem item) {  
		    switch (item.getItemId()) {  
		        case MENU_ITEM_0:  
		    		Intent gomapa=new Intent(Mapa.this, Home.class);
		    		startActivity(gomapa);		  
		    		finish();
		    		break;
		        case MENU_ITEM_1: 
		    		Intent goviaje=new Intent(Mapa.this, Viaje.class);
		    		startActivity(goviaje);
		        	break;
		        case MENU_ITEM_2:  
		    		Intent goconfig=new Intent(Mapa.this, Configuracion.class);
		    		startActivity(goconfig);
		        	break;
		        case MENU_ITEM_3: 
		    		Intent gocam=new Intent(Mapa.this, Camara.class);
		    		startActivity(gocam);	        	
		    		break;
		        case MENU_ITEM_4:  
		    		Intent gomodo=new Intent(Mapa.this, Modo.class);
		    		startActivity(gomodo);

			  default:
		    }  
		    return false;  
		} 
	 
	 
	  
	private GeoPoint getPoint(double lat, double lon) {
		    return(new GeoPoint((int)(lat*1000000.0),
		                          (int)(lon*1000000.0)));
	} 
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.mapa);
	    map=(MapView)findViewById(R.id.map);
	    map.getController().setCenter(getPoint(19.434887,-99.141333));
	    map.getController().setZoom(17);
	    map.setBuiltInZoomControls(true);
	    
	    Drawable marker=getResources().getDrawable(R.drawable.marker);
	    
	    marker.setBounds(0, 0, marker.getIntrinsicWidth(),
	                            marker.getIntrinsicHeight());
	    
	    map.getOverlays().add(new SitesOverlay(marker));
	    
	    me=new MyLocationOverlay(this, map);
	    map.getOverlays().add(me);
	    
	    Button btnFiltrar=(Button)findViewById(R.id.filtrar);
	    btnFiltrar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent goFiltro=new Intent(Mapa.this, Filtrar.class);
				startActivity(goFiltro);
//				finish();
			}
		});
	    
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	  private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
		    private List<OverlayItem> items=new ArrayList<OverlayItem>();
		    private Drawable marker=null;
		    
		    public SitesOverlay(Drawable marker) {
		      super(marker);
		      this.marker=marker;
		      
		      boundCenterBottom(marker);
		      
		      items.add(new OverlayItem(getPoint(19.434887,-99.141333),
		                                "Palacio de Bellas Artes", "Palacio de Bellas Artes"));
		      items.add(new OverlayItem(getPoint(19.432661,-99.133303),
		                                "Zócalo",
		                                "Zócalo"));
		      items.add(new OverlayItem(getPoint(19.435626,-99.140378),
		                                "Palacio Postal",
		              "Palacio Postal"));
		      items.add(new OverlayItem(getPoint(19.433941,-99.140571),
		                                "Torre Latinoamericana",
		                        "Torre Latinoamericana"));

		      populate();
		    }
		    
		    @Override
		    protected OverlayItem createItem(int i) {
		      return(items.get(i));
		    }
		    
		    @Override
		    protected boolean onTap(int i) {
		    	Toast.makeText(Mapa.this,

		    			items.get(i).getSnippet(),
		                      Toast.LENGTH_LONG).show();



		      return(true);
		    }
		    
		    @Override
		    public int size() {
		      return(items.size());
		    }
		  }
}
